package com.hdaccp.goodsprovider.controller;

import com.hdaccp.goodsprovider.dao.TypeDao;
import org.main.bean.POJO.PType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/typeinfo")
public class TypeController {

    @Autowired
    private TypeDao typeDao;

    @RequestMapping("/query")
    public List<PType>query(){
        return  typeDao.findAll();
    }

    @RequestMapping("/getOneId/{id}")
    public PType getOneId(@PathVariable("id") int id){
        return typeDao.getOne(id);
    }

    //增加
    @RequestMapping("/save/{id}/{size}")
    public String save(@PathVariable("id") int id,@PathVariable("size") String  size){
        String msg="增加商品类型成功";

        PType pType=new PType();
        pType.setId(id);
        pType.setSize(size);

        try{
            typeDao.save(pType);
        }catch (Exception e){
            msg="增加商品类型失败";
        }
       return msg;
    }

    @RequestMapping("/update/{id}/{size}")
    public String update(@PathVariable("id") int id,@PathVariable("size") String  size){
        String msg="修改商品类型成功";
        PType pType=typeDao.getOne(id);
        pType.setSize(size);

        try{
            typeDao.save(pType);
        }catch (Exception e){
            msg="修改商品类型失败";
        }
        return msg;
    }

    @RequestMapping("/del{id}")
    public String del(@PathVariable("id") int id){
        String msg="删除商品类型成功";

        try{
            typeDao.getOne(id);
        }catch (Exception e){
            msg="删除商品类型失败";
        }
        return msg;
    }
}
